﻿using Abp.Domain.Entities;
using Abp.Domain.Repositories;
using Abp.Domain.Services;
using Abp.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace shenghua.Common.Articles
{
    public class ArticleManager: DomainService
    {
        private readonly IRepository<Article> _articleRepository;
        private readonly IRepository<ArticleComment> _articleCommentRepository;

        public ArticleManager(
            IRepository<Article> articleRepository,
            IRepository<ArticleComment> articleCommentRepository)
        {
            _articleRepository = articleRepository;
            _articleCommentRepository = articleCommentRepository;
        }

        public async Task<Article> Get(int articleId, bool mustBeEnabled = false)
        {
            var item = await _articleRepository.FirstOrDefaultAsync(articleId);
            if(item == null)
            {
                throw new UserFriendlyException("文章不存在");
            }
            if(mustBeEnabled)
            {
                if(item.Enabled == false)
                {
                    throw new UserFriendlyException("未启用的文章不可访问");
                }
            }
            return item;
        }

        public async Task ClickArticle(Article article)
        {
            article.ClickCount += 1;
        }

        // 新增，删除，审核，编辑
        public async Task<Article> Create(Article article)
        {
            if(article.Status != ArticleStatus.Audited)
            {
                article.Enabled = false;
            }
            await _articleRepository.InsertAndGetIdAsync(article);
            return article;
        }

        public async Task<Article> Update(Article article)
        {
            if (article.Status != ArticleStatus.Audited)
            {
                article.Enabled = false;
            }
            return article;
        }

        public async Task Delete(int articleId)
        {
            await _articleRepository.DeleteAsync(articleId);
        }

        public async Task AuditArticle(int articleId)
        {
            var item = await Get(articleId);
            item.Status = ArticleStatus.Audited;
        }

        public async Task Enabled(int articleId, bool value)
        {
            var item = await Get(articleId);
            if(value == true && item.Status != ArticleStatus.Audited)
            {
                throw new UserFriendlyException("未通过审核的文章不可设为对外可见");
            }
            item.Enabled = value;
        }


        public async Task<ArticleComment> CommentArticle(int articleId, long userId, string content)
        {
            ArticleComment articleComment = new ArticleComment
            {
                TargetId = articleId,
                UserId = userId,
                Content = content
            };
            return await _articleCommentRepository.InsertAsync(articleComment);
        }

        public async Task<ArticleComment> CommentOther(int commentId, long userId, string content)
        {
            ArticleComment targetComment = _articleCommentRepository.Get(commentId);

            ArticleComment articleComment = new ArticleComment
            {
                TargetId = targetComment.TargetId,
                ParentCommentId = commentId,
                UserId = userId,
                Content = content
            };
            return await _articleCommentRepository.InsertAsync(articleComment);
        }
    }
}
